package com.rainier.action;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.rainier.bean.User;
import com.rainier.service.UserService;

@Controller
@Scope("prototype")
public class UserAction extends ActionSupport
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String username;
	
	private String password;
	
	private UserService userService;
	
	@Resource(name="userService")
	public void setUserService(UserService userService)
	{
		this.userService = userService;
	}

	public String getUsername()
	{
		return username;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}
	
	//验证用户名是否存在
	public String checkUsername() throws Exception
	{
		return SUCCESS;
	}
	
	//登陆
	public String checkUser() throws Exception
	{
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		
		if(userService.checkUser(user))
		{
			HttpSession session = ServletActionContext.getRequest().getSession();
			session.setAttribute("username", username);
			
			return SUCCESS;
		}
		else
		{
			HttpServletRequest request = ServletActionContext.getRequest();
			request.setAttribute("error","用户名或密码错误" );
			return INPUT;
		}
	}
	
	//注册用户
	public String addUser() throws Exception
	{
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		
		userService.addUser(user);
		
		return SUCCESS;
	}
	
}
